package pl.edu.agh.gameserv.server.test;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;

import pl.edu.agh.gameserv.server.test.helper.User;

public class WonGameIT {
	ServerImplService service;
	

	@Test
	public void wonGame1() throws Exception {
		String result1[][] = { { "0", "1", " " }, { "1", "0", " " }, { " ", " ", "0" } };
		service = new ServerImplService();
		ServerImpl server = service.getServerImplPort();

		User user1 = new User("Bolek", server);
		user1.joinGame();

		User user2 = new User("Lolek", server);
		user2.joinGame();

		user1.makeMove("00");
		user2.makeMove("01");
		user1.makeMove("11");
		user2.makeMove("10");
		user1.makeMove("22");

		assertEquals(GameStatus.FINISHED, user1.getState());
		assertEquals(GameStatus.FINISHED, user2.getState());
		assertEquals("User 0 won.", user1.getScore());

		List<StringArray> situation = user1.getSituation();
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				assertEquals(result1[j][i], situation.get(i).getItem().get(j));
	}
	
	@Test
	public void wonGame2() throws Exception {
		String result1[][] = { { "0", "0", "0" }, { "1", " ", " " }, { " ", "1", " " } };
		service = new ServerImplService();
		ServerImpl server = service.getServerImplPort();

		User user1 = new User("Bolek", server);
		user1.joinGame();

		User user2 = new User("Lolek", server);
		user2.joinGame();

		user1.makeMove("00");
		user2.makeMove("01");
		user1.makeMove("10");
		user2.makeMove("12");
		user1.makeMove("20");

		assertEquals(GameStatus.FINISHED, user1.getState());
		assertEquals(GameStatus.FINISHED, user2.getState());
		assertEquals("User 0 won.", user1.getScore());

		List<StringArray> situation = user1.getSituation();
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				assertEquals(result1[j][i], situation.get(i).getItem().get(j));
	}
	
	@Test
	public void wonGame3() throws Exception {
		String result1[][] = { { "0", "1", " " }, { "0", " ", " " }, { "0", "1", " " } };
		service = new ServerImplService();
		ServerImpl server = service.getServerImplPort();

		User user2 = new User("Lolek", server);
		user2.joinGame();

		User user1 = new User("Bolek", server);
		user1.joinGame();
		
		user2.makeMove("00");
		user1.makeMove("10");
		user2.makeMove("01");
		user1.makeMove("12");
		user2.makeMove("02");

		assertEquals(GameStatus.FINISHED, user1.getState());
		assertEquals(GameStatus.FINISHED, user2.getState());
		assertEquals("User 0 won.", user1.getScore());

		List<StringArray> situation = user1.getSituation();
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				assertEquals(result1[j][i], situation.get(i).getItem().get(j));
	}
	
	@Test
	public void wonGame4() throws Exception {
		String result1[][] = { { " ", "0", "1" }, { "0", "1", " " }, { "1", "0", " " } };
		service = new ServerImplService();
		ServerImpl server = service.getServerImplPort();

		User user1 = new User("Bolek", server);
		user1.joinGame();
		
		User user2 = new User("Lolek", server);
		user2.joinGame();
		
		user1.makeMove("10");
		user2.makeMove("02");
		user1.makeMove("01");
		user2.makeMove("11");
		user1.makeMove("12");
		user2.makeMove("20");

		assertEquals(GameStatus.FINISHED, user1.getState());
		assertEquals(GameStatus.FINISHED, user2.getState());
		assertEquals("User 1 won.", user1.getScore());

		List<StringArray> situation = user1.getSituation();
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				assertEquals(result1[j][i], situation.get(i).getItem().get(j));
	}

	@Test
	public void wonGame5() throws Exception {
		String result1[][] = { { "1", "0", "1" }, { "0", "1", " " }, { "0", "0", "1" } };
		service = new ServerImplService();
		ServerImpl server = service.getServerImplPort();

		User user0 = new User("Bolek", server);
		user0.joinGame();
		
		User user1 = new User("Lolek", server);
		user1.joinGame();
		
		user0.makeMove("10");
		user1.makeMove("00");
		user0.makeMove("01");
		user1.makeMove("20");
		user0.makeMove("02");
		user1.makeMove("11");
		
		user0.makeMove("12");
		user1.makeMove("22");

		assertEquals(GameStatus.FINISHED, user0.getState());
		assertEquals(GameStatus.FINISHED, user1.getState());
		assertEquals("User 1 won.", user0.getScore());

		List<StringArray> situation = user0.getSituation();
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				assertEquals(result1[j][i], situation.get(i).getItem().get(j));
	}

	@Test
	public void wonGame6() throws Exception {
		String result1[][] = { { "1", " ", "0" }, { "1", " ", "0" }, { " ", " ", "0" } };
		service = new ServerImplService();
		ServerImpl server = service.getServerImplPort();

		User user0 = new User("Bolek", server);
		user0.joinGame();
		
		User user1 = new User("Lolek", server);
		user1.joinGame();
		
		user0.makeMove("20");
		user1.makeMove("00");
		user0.makeMove("21");
		user1.makeMove("01");
		user0.makeMove("22");
		

		assertEquals(GameStatus.FINISHED, user0.getState());
		assertEquals(GameStatus.FINISHED, user1.getState());
		assertEquals("User 0 won.", user0.getScore());

		List<StringArray> situation = user0.getSituation();
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				assertEquals(result1[j][i], situation.get(i).getItem().get(j));
	}

	
}